import Vue from 'vue'
import VueRouter from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: "/login"
  },
  {
    path: '/login',
    name: 'Login',
    meta: { title: "登录" },

    component: () => import('@/views/Login.vue')
  },
  {
    path: "/home",
    name: "Home",
    meta: { title: "首页" },
    component: () => import("@/views/Home"),
    children: [
      {
        path: "",
        redirect: "table"
      },
      {
        path: "table",
        name: "Table",
        meta: { title: "统计表" },
        component: () => import("@/components/StatisticalTable")
      },
      {
        path: "chart",
        name: "chart",
        meta: { title: "统计图" },
        component: () => import("@/components/StatisticalChart")
      },
      {
        path: "team",
        name: "team",
        meta: { title: "家庭医生团队管理" },
        component: () => import("@/components/TeamManagement"),
      },
      {
        path: "team/editTeamManagement",
        name: "EditTeamManagement",
        meta: { title: "修改家庭医生团队管理信息" },
        component: () => import("@/components/EditTeamManagement")
      },
      {
        path:"team/addTeamManagement",
        name:"AddTeamManagement",
        meta:{titel:"添加家庭医生团队"},
        component:()=>import("@/components/AddTeamManagement")
      },
      {
        path: "doctor",
        name: "doctor",
        meta: { title: "家庭医生管理" },
        component: () => import("@/components/DoctorManagement")
      },
      {
        path: "doctor/editDoctor",
        name: "EditDoctor",
        meta: { title: "修改家庭医生信息" },
        component: () => import("@/components/EditDoctor")
      },
      {
        path: "contract",
        name: "contract",
        meta: { title: "家庭医生签约管理" },
        component: () => import("@/components/ContractManagement")
      },
      {
        path:"contract/editContract",
        name:"EditContract",
        meta:{title:"修改家庭医生签约管理信息"},
        component:()=>import ("@/components/EditContractManagement")
      }
    ]
  }
]


const router = new VueRouter({
  routes,
  mode: 'history'
})

router.beforeEach((to, from, next) => {
  const isLogin = sessionStorage.getItem("isLogin")
  if (isLogin || to.path === "/login") {
    document.title = to.meta.title
    sessionStorage.setItem("title", to.meta.title)
    NProgress.start()
    next()
  } else {
    next("/login")
  }

})
router.afterEach(() => {
  NProgress.done();
});

export default router
